package net.contrend.system.action;

import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.contrend.system.dao.MenuDao;
import net.contrend.system.dao.RoleMenuDao;
import net.contrend.system.dao.UserDao;
import net.contrend.system.dao.UserRoleDao;
import net.contrend.system.form.MenuForm;
import net.contrend.system.form.UserForm;
import net.contrend.util.BaseAction;
import net.contrend.util.Constants;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction extends BaseAction {
    
    private String loginPage = "login";
    private String indexPage = "index";
    
    private UserDao userDao;
    private MenuDao menuDao;
    private UserRoleDao userRoleDao;
    private RoleMenuDao roleMenuDao;
    
    private void initDao() {
        if (null == this.userDao) {
            this.userDao = (UserDao) this.getBean("userDao");
        }
        if (null == this.menuDao) {
            this.menuDao = (MenuDao) this.getBean("menuDao");
        }
        if (null == this.userRoleDao) {
            this.userRoleDao = (UserRoleDao) this.getBean("userRoleDao");
        }
        if (null == this.roleMenuDao) {
            this.roleMenuDao = (RoleMenuDao) this.getBean("roleMenuDao");
        }
    }
    
    public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        this.initDao();
        
        String loginName = request.getParameter("loginName");
        String loginPassword = request.getParameter("loginPassword");
        
        if (StringUtils.isNotBlank(loginName) && StringUtils.isNotBlank(loginPassword)) {
            UserForm user = this.userDao.query(loginName, loginPassword);
            if (null != user) {
                List<MenuForm> menus = null;
                if (Constants.SYSEM_LOGIN_ID.equals(user.getUserId())) {
                    menus = this.menuDao.getMenus();
                } else {
                    List<String> roleIds = this.userRoleDao.getRoleIds(user.getUserId(), true);
                    menus = this.roleMenuDao.getMenus(roleIds.toArray(new String[roleIds.size()]));
                }
                
                request.getSession().setAttribute("menus", menus);
                request.getSession().setAttribute(Constants.USER_LOGIN_ID, user);
                return mapping.findForward(this.indexPage);
            }
        } else {
            request.setAttribute("ACTION_RESULT", 0);
        }
        return mapping.findForward(this.loginPage);
    }
    
    @SuppressWarnings("unchecked")
    public ActionForward logout(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        this.initDao();
        
        HttpSession session = request.getSession();
        Enumeration<String> names = session.getAttributeNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            session.removeAttribute(name);
        }
        
        return mapping.findForward(this.loginPage);
    }
    
}
